package com.just.school_campus.service.impl;

import com.just.school_campus.dao.GoodsMapper;
import com.just.school_campus.dao.OrderMapper;
import com.just.school_campus.dao.ShopCarMapper;
import com.just.school_campus.model.*;
import com.just.school_campus.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderDao;
    @Autowired
    private GoodsMapper goodsDao;
    @Autowired
    private ShopCarMapper shopCarDao;

    @Override
    public List<Order> getOrdersAllByUserId(String userId) {
        OrderExample example = new OrderExample();
        example.createCriteria().andUserIdEqualTo(userId);
        return orderDao.selectByExample(example);
    }

    @Override
    public int createOrder(Order order) {
        synchronized (this) {
            while (true) {
                String ordersId = UUID.randomUUID().toString();
                if (orderDao.selectByPrimaryKey(ordersId) == null) {
                    GoodsExample example = new GoodsExample();
                    example.createCriteria().andGoodsIdEqualTo(order.getGoodsId())
                            .andUpStatusEqualTo(1)
                            .andUseStatusEqualTo(1);
                    Goods goods = goodsDao.selectByExample(example).get(0);
                    order.setOrdersId(ordersId);
                    order.setPayId(UUID.randomUUID().toString());
                    order.setPrice(goods.getPrice());
                    order.setTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
                    return orderDao.insertSelective(order);
                }
            }
        }
    }

    @Override
    public int createOrders(Order order) {
        boolean flag = true;
        ShopCarExample example = new ShopCarExample();
        example.createCriteria().andUserIdEqualTo(order.getUserId());
        List<ShopCar> allByUid = shopCarDao.selectByExample(example);
        for(ShopCar shopCar : allByUid){
            GoodsExample goodsExample = new GoodsExample();
            goodsExample.createCriteria().andGoodsIdEqualTo(shopCar.getGoodsId())
                    .andUpStatusEqualTo(1)
                    .andUseStatusEqualTo(1);
            Goods goods = goodsDao.selectByExample(goodsExample).get(0);
            synchronized (this) {
                while (true) {
                    String ordersId = UUID.randomUUID().toString();
                    OrderExample orderExample = new OrderExample();
                    orderExample.createCriteria().andOrdersIdEqualTo(ordersId);
                    if (orderDao.selectByExample(orderExample).size() == 0) {
                        order.setGoodsId(goods.getGoodsId());
                        order.setOrdersId(ordersId);
                        order.setPayId(UUID.randomUUID().toString());
                        order.setPrice(goods.getPrice());
                        order.setTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
                        if(orderDao.insertSelective(order) != 1) {
                            flag = false;
                        }
                        break;
                    }
                }
            }
            if(!flag){
                return 0;
            }
        }
        return 1;
    }
}
